#include <llist.h>

void show(void *data, void *arg)
{
    printf("%d ", *(int *)data);
}

int cmp(const void *d1, const void *d2)
{
    return *(int *)d1 - *(int *)d2;
}

int main(int argc, char *argv[])
{
    int n;
    LLIST *handle = NULL;
    int *p = NULL;

    if (argc > 1)
        handle = llist_load(argv[1], show, cmp, NULL, NULL, NULL);
    else
    {
        handle = llist_create(sizeof(int), show, cmp, NULL, NULL);

        while (1)
        {
            KYO_GETVAL(n, "%d", "please input num: ");

            if (n == -1)
                break;
            llist_append(&n, handle);
        }
        llist_store("./db", handle);
        printf("store: ");
        llist_travel(NULL, handle);
        printf("\n");

        llist_rev(handle);
        printf("rev: ");
        while ((p = llist_travel_data(handle)) != NULL)
            printf("%d ", *p);
        printf("\n");

        llist_travel_rewind(handle);

        printf("rev: ");
        while ((p = llist_travel_data(handle)) != NULL)
            printf("%d ", *p);
        printf("\n");
    }

    n = 8;
    if (llist_find(&n, 0, handle) > 0)
    {
        printf("find 8: \n");
        llist_travel_find(NULL, handle);
        printf("\n");
    }

    n = 8;
    llist_delete(&n, 0, handle);
    printf("delete 8(0): ");
    llist_travel(NULL, handle);
    printf("\n");

    n = 3;
    llist_delete(&n, -2, handle);
    printf("delete 3(-2): ");
    llist_travel(NULL, handle);
    printf("\n");

    n = 5;
    llist_delete(&n, 3, handle);
    printf("delete 5(3): ");
    llist_travel(NULL, handle);
    printf("\n");

    llist_destroy(&handle);

    return 0;
}
